package 
{
	import away3d.containers.ObjectContainer3D;
	import away3d.core.base.Geometry;
	import away3d.entities.Mesh;
	import away3d.entities.SegmentSet;
	import away3d.materials.lightpickers.StaticLightPicker;
	import away3d.materials.TextureMaterial;
	import away3d.primitives.LineSegment;
	import away3d.utils.Cast;
	import flash.geom.Vector3D;
	
	/**
	 * ...
	 * @author 
	 */
	public class Star extends ObjectContainer3D
	{
		private var _distance : Number;
		private var _rotSpeed : Number;
		private var _orbit : Orbit;
		
		public function Star(geometry : Geometry, embeddedImage : Class, lightPicker : StaticLightPicker = null, orbit : Orbit = null, rotSpeed : Number = 0, distance : Number = 0) : void
		{
			super();
			_orbit = orbit;
			_distance = distance;
			_rotSpeed = rotSpeed;
			init(geometry, embeddedImage, lightPicker);
		}
		
		private function init(geometry : Geometry, embeddedImage : Class, lightPicker : StaticLightPicker) : void
		{
			var _material : TextureMaterial = new TextureMaterial(Cast.bitmapTexture(embeddedImage));
			if (lightPicker) { _material.lightPicker = lightPicker; }

			if (_orbit) { _orbit.scale = _distance; }
			
			addChild(new Mesh(geometry, _material));
			
		}
		
		/**
		 * Posición en la orbita según el tiempo transcurrido
		 * @param	time, tiempo transcurrido
		 */
		public function setPosition(time : Number) : void
		{
			if (_orbit)
			{
				var v : Number = _orbit.trueAnomaly(time);
				var r : Number = _orbit.radialDistance(v);
				
				this.x = r * Math.cos(v);
				this.z = r * Math.sin(v);
			}
		}
		
		/**
		 * Retorna la orbita en segmentos para ser dibujada
		 * @param	segments, cantidad de segmentos
		 * @param	thickness, grueso de la linea
		 * @return segmentset con las partes
		 */
		public function getOrbitSegmentSet(segments : Number, thickness : Number) : SegmentSet
		{
			var path : Vector.<Vector3D> = _orbit.getPath(segments);
			var lines : SegmentSet = new SegmentSet();
						
			for (var i:int = 0; i < path.length-1; i++)
			{
				lines.addSegment(new LineSegment(path[i], path[i+1], 0xffffff, 0xffffff, thickness));
			}
			return lines;
		}
		
	}
	
}